package com.qf.service.impl;

import com.qf.common.dto.MemberRequest;
import com.qf.common.utils.DigestsUtil;
import com.qf.common.utils.SaltUtil;
import com.qf.entity.Member;
import com.qf.mapper.MemberMapper;
import com.qf.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;
import java.util.UUID;

/**
 * @author KING
 * @ClassName: com.qf.service.impl.UserServiceImpl
 * @Description: 该类的功能描述
 * @date: 2021/10/22 14:02
 */
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;

    @Override
    public Member login(String username) {
        Member member = memberMapper.selectMember(username);
        return member;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean register(MemberRequest memberRequest) {
        //明文密码  md5+salt+hash散列
        Member member = new Member();
        BeanUtils.copyProperties(memberRequest, member);
        //生成随即盐
        String salt = SaltUtil.getSalt(8);
        member.setSalt(salt);
        //hash散列，指定散列次数
        Md5Hash md5Hash = new Md5Hash(member.getPassword(), salt, 1024);

        member.setPassword(md5Hash.toHex());
        Integer insert = memberMapper.insertMember(member);
        return insert > 0;
    }

    @Override
    public Boolean updateMember(MemberRequest memberRequest) {
        return memberMapper.updateMember(memberRequest);
    }
}
